/** 
 * @Title: UserDAOImpl.java 
 * @Package com.prs.um.usermanager.dao.impl 
 * @Description: usermanager DAO层
 * @author flybonew@163.com 
 * @date 2013-1-7 上午10:52:08 
 * @version V1.0 
 */  
package com.prs.um.usermanager.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.prs.um.deptmanager.dto.Dept;
import com.prs.um.dutymanager.dto.Duty;
import com.prs.um.usermanager.dao.UserDAO;
import com.prs.um.usermanager.dto.User;

/** 
 * @ClassName: UserDAOImpl 
 * @Description: User 的DAO层
 * @author flybonew@163.com
 * @date 2013-1-7 上午10:52:08 
 * 
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class UserDAOImpl implements UserDAO {
	
	@Resource
	private HibernateTemplate hibernateTemplate;
	
	/*
	 * @Description: 添加用户 
	 * @author flybonew@163.com
	 * @date 2013-1-8 上午9:56:48 
	 */
	@Override
	public User addUser(User user) {
		this.hibernateTemplate.save(user);
		return user;
	}

	/*
	 * @Description: 更新用户 
	 * @author flybonew@163.com
	 * @date 2013-1-8 上午9:56:48 
	 */
	@Override
	public void updateUser(User user) {
		this.hibernateTemplate.update(user);
	}

	/*
	 * @Description: 删除用户
	 * @author flybonew@163.com
	 * @date 2013-1-8 上午9:56:48 
	 */
	@Override
	public void deleteUser(User user) {
		this.hibernateTemplate.delete(user);
	}

	/*
	 * @Description: 通过用户ID查询用户
	 * @author flybonew@163.com
	 * @date 2013-1-8 上午9:56:48 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public User seleteUserById(String userId) {
		StringBuffer sql = new StringBuffer();
		sql.append("from User where 1=1 ");
		sql.append("and id_ = '"+ userId+"'");
		final String typeSql = new String(sql);
		List<User> list = (List<User>)this.hibernateTemplate.find(typeSql);
		if(null != list && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	/*
	 * @Description: 通过loginId查询用户 
	 * @author flybonew@163.com
	 * @date 2013-1-8 上午9:56:48 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public User seleteUserByLoginId(String loginId) {
		StringBuffer sql = new StringBuffer();
		sql.append("from User where 1=1 ");
		sql.append("and login_id_ = '"+ loginId+"'");
		final String typeSql = new String(sql);
		List<User> list = (List<User>)this.hibernateTemplate.find(typeSql);
		if(null != list && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	/*
	 * @Description: 通过部门的ID和状态查询用户
	 * @author flybonew@163.com
	 * @date 2013-1-8 上午9:56:48 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> selectUserByDeptId(String deptId, Integer state) {
		StringBuffer sql = new StringBuffer();
		sql.append("from Dept where 1=1 ");
		sql.append("and id_ = '"+ deptId+"'");
		final String typeSql = new String(sql);
		List<Dept> list = (List<Dept>)this.hibernateTemplate.find(typeSql);
		if(null != list && list.size() > 0){
			Dept dept = list.get(0);
			Set<User> users = dept.getDeptMember();
			List<User> userList = new ArrayList<User>();
			for (Iterator<User> iterator = users.iterator(); iterator.hasNext();) {
				User user = (User) iterator.next();
				if(null != state && user.getState() == state){
					userList.add(user);
				}else if(null == state){
					userList.add(user);
				}
			}
			return userList;
		}
		return null;
	}

	/*
	 * @Description: 通过职务ID查询用户
	 * @author flybonew@163.com
	 * @date 2013-1-8 上午9:56:48 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> selectUserByDutyId(String dutyId, Integer state) {
		StringBuffer sql = new StringBuffer();
		sql.append("from Duty where 1=1 ");
		sql.append("and id_ = '"+ dutyId+"'");
		final String typeSql = new String(sql);
		List<Duty> list = (List<Duty>)this.hibernateTemplate.find(typeSql);
		if(null != list && list.size() > 0){
			Duty duty = list.get(0);
			Set<User> users = duty.getUserList();
			List<User> userList = new ArrayList<User>();
			for (Iterator<User> iterator = users.iterator(); iterator.hasNext();) {
				User user = (User) iterator.next();
				if(null != state && user.getState() == state){
					userList.add(user);
				}else if(null == state){
					userList.add(user);
				}
			}
			return userList;
		}
		return null;
	}

	/*
	 * @Description: 通过条件查询 
	 * @author flybonew@163.com
	 * @date 2013-1-10 下午3:32:26 
	 */
	@Override
	public List<User> selectUserByCondition(Map<String, Object> map) {
		StringBuffer sql = new StringBuffer();
		sql.append("from Duty where 1=1 ");
		if(map.containsKey("dutyName")){
			String dutyName = (String)map.get("dutyName");
			//sql.append("and id_ = '"+ dutyId+"'");
		}
		this.hibernateTemplate.executeFind(new HibernateCallback<Object>() {

			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				return null;
			}
		});
		return null;
	}

}
